package com.ecube.test;

import com.alibaba.fastjson.JSON;
import com.ecube.mqtt.MqttUtils;
import com.ecube.protocol.ProtocolHelper;
import com.ecube.protocol.common.DataReportHead;
import com.ecube.protocol.common.DataReportStandard;
import com.ecube.protocol.enums.StorageFunctionEnum;
import com.ecube.protocol.storage.StorageBatInfo;
import com.ecube.protocol.storage.StoragePCSInfo;
import com.ecube.protocol.storage.StoragePCSState;
import com.ecube.utils.DateUtils;
import com.ecube.utils.EnumUtils;
import lombok.Builder;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * 储能
 *
 * @author zhuweijun
 * @version 1.0.0
 * @since 2025/8/4 17:20
 */
public class SendStorageInfo {

    public static void main(String[] args) throws Exception {
        //sendStorageInfo("FAT", "100100001", "101202508", PCSInfo.builder().prefixNo("101202508").size(2).build());
        //sendStorageBatInfo("FAT", "100100001", "101202508", PCSInfo.builder().prefixNo("101202508").size(2).build());

        //sendStorageInfo("FAT", "100100002", "218888800521", PCSInfo.builder().prefixNo("218888800").size(2).build());

        sendStorageInfo("FAT", "100100001", "101202509", PCSInfo.builder().prefixNo("101202509").size(2).batIdPrefix("1256010210").build());
        sendStorageBatInfo("FAT", "100100001", "101202509", PCSInfo.builder().prefixNo("101202509").size(2).batIdPrefix("1257010210").build());

        sendStorageInfo("FAT", "100100001", "CN202501020", PCSInfo.builder().prefixNo("101202509").size(2).batIdPrefix("1256010210").build());
        while (true){
            //sendStoragePCSState("FAT", "100100001", "101202508", PCSInfo.builder().prefixNo("101202508").size(2).build());
            sendStoragePCSState("FAT", "100100001", "101202509", PCSInfo.builder().prefixNo("101202509").size(2).build());
            Thread.sleep(1000*10);
        }
    }

    /**
     * 发送PCS属性报文
     *
     * @param env env
     * @param productCode productCode
     * @param deviceCode deviceCode
     */
    private static void sendStorageInfo(String env, String productCode, String deviceCode, PCSInfo info) {
        StorageFunctionEnum function = (StorageFunctionEnum) EnumUtils.getFunction(StoragePCSInfo.class);
        if(function == null){
            return;
        }

        String pubTopic = productCode + "/" + deviceCode + "/" + function.getDirection() + "/" + function.getType();

        DataReportStandard reportStandard = new DataReportStandard();
        DataReportHead reportHand = ProtocolHelper.getReportHand(productCode, deviceCode, function.getFunction());
        reportHand.setVersion("V1.0.1");
        reportStandard.setHeader(reportHand);

        List<StoragePCSInfo> pcsInfoList = new ArrayList<>();
        for (Integer i = 1; i <= info.getSize(); i++) {
            StoragePCSInfo pcsInfo = new StoragePCSInfo();
            pcsInfo.setPcsNo(String.valueOf(i));
            pcsInfoList.add(pcsInfo);
        }
        reportStandard.setDataBody(pcsInfoList);

        String reportContent = JSON.toJSONString(reportStandard);
        System.out.println("发送报文内容：" + reportContent);
        MqttUtils.sendMQTT(env, pubTopic, reportContent);


        // 心跳空报文
        reportHand.setFunction("keepalive");
        DataReportStandard reportStandard1 = new DataReportStandard();
        reportStandard1.setHeader(reportHand);
        reportStandard1.setDataBody(new Object());
        MqttUtils.sendMQTT(env, pubTopic, JSON.toJSONString(reportStandard1));
    }

    /**
     * PCS状态
     *
     * @param env env
     * @param productCode productCode
     * @param deviceCode deviceCode
     * @param info info
     */
    private static void sendStoragePCSState(String env, String productCode, String deviceCode, PCSInfo info) {
        StorageFunctionEnum function = (StorageFunctionEnum) EnumUtils.getFunction(StoragePCSState.class);
        if(function == null){
            return;
        }

        String pubTopic = productCode + "/" + deviceCode + "/" + function.getDirection() + "/" + function.getType();

        DataReportStandard reportStandard = new DataReportStandard();
        DataReportHead reportHand = ProtocolHelper.getReportHand(productCode, deviceCode, function.getFunction());
        reportHand.setVersion("V1.0.1");
        reportStandard.setHeader(reportHand);

        List<StoragePCSState> pcsStateList = new ArrayList<>();
        for (Integer i = 1; i <= info.getSize(); i++) {
            StoragePCSState pcsState = new StoragePCSState();
            pcsState.setPcsNo(String.valueOf(i));
            pcsStateList.add(pcsState);
        }
        reportStandard.setDataBody(pcsStateList);

        String reportContent = JSON.toJSONString(reportStandard);
        System.out.println("发送报文内容：" + reportContent);
        MqttUtils.sendMQTT(env, pubTopic, reportContent);
    }

    /**
     * 电池属性
     *
     * @param env env
     * @param productCode productCode
     * @param deviceCode deviceCode
     * @param info info
     */
    private static void sendStorageBatInfo(String env, String productCode, String deviceCode, PCSInfo info){
        StorageFunctionEnum function = (StorageFunctionEnum) EnumUtils.getFunction(StorageBatInfo.class);
        if(function == null){
            return;
        }

        String pubTopic = productCode + "/" + deviceCode + "/" + function.getDirection() + "/" + function.getType();

        DataReportStandard reportStandard = new DataReportStandard();
        DataReportHead reportHand = ProtocolHelper.getReportHand(productCode, deviceCode, function.getFunction());
        reportHand.setVersion("V1.0.1");
        reportStandard.setHeader(reportHand);

        List<StorageBatInfo> batInfoList = new ArrayList<>();
        for (Integer i = 1; i <= info.getSize(); i++) {
            StorageBatInfo batInfo = new StorageBatInfo();
            batInfo.setPcsNo(String.valueOf(i));
            batInfo.setBatID(info.getBatIdPrefix() + i);
            batInfoList.add(batInfo);
        }
        reportStandard.setDataBody(batInfoList);

        String reportContent = JSON.toJSONString(reportStandard);
        System.out.println("发送报文内容：" + reportContent);
        MqttUtils.sendMQTT(env, pubTopic, reportContent);
    }

    @Data
    @Builder
    private static class PCSInfo {

        /**
         * 编号前缀
         */
        private String prefixNo;

        /**
         * 个数
         */
        private Integer size;

        /**
         * 电池编号前缀
         */
        private String batIdPrefix;

    }
}
